<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生姓名" prop="tstgXsxm">
        <el-input
          v-model="queryParams.tstgXsxm"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生校号" prop="tstgXsxh">
        <el-input
          v-model="queryParams.tstgXsxh"
          placeholder="请输入学生校号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属学院" prop="tstgSsxy">
        <el-input
          v-model="queryParams.tstgSsxy"
          placeholder="请输入所属学院"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所学专业" prop="tstgSxzy">
        <el-input
          v-model="queryParams.tstgSxzy"
          placeholder="请输入所学专业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学社类别" prop="tstgXslb">
        <el-input
          v-model="queryParams.tstgXslb"
          placeholder="请输入学社类别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学社名称" prop="tstgXsmc">
        <el-input
          v-model="queryParams.tstgXsmc"
          placeholder="请输入学社名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="岗位名称" prop="tstgGwmc">
        <el-input
          v-model="queryParams.tstgGwmc"
          placeholder="请输入岗位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="岗位等级" prop="tstgGwdj">
        <el-input
          v-model="queryParams.tstgGwdj"
          placeholder="请输入岗位等级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['admin:tstg:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['admin:tstg:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['admin:tstg:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['admin:tstg:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tstgList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="学生姓名" align="center" prop="tstgXsxm" />
      <el-table-column label="学生校号" align="center" prop="tstgXsxh" />
      <el-table-column label="所属学院" align="center" prop="tstgSsxy" >
        <template slot-scope="scope">
          {{ getdeptLabel(scope.row.tstgSsxy) }}
        </template>
      </el-table-column>
      <el-table-column label="所学专业" align="center" prop="tstgSxzy" />
      <el-table-column label="学社类别" align="center" prop="tstgXslb" >
        <template slot-scope="scope">
        {{ getLabel(scope.row.tstgXslb) }} 
        </template>
      </el-table-column>
      <el-table-column label="学社名称" align="center" prop="tstgXsmc" >
        <template slot-scope="scope">
        {{ getxsxxLabel(scope.row.tstgXsmc) }} 
        </template>
      </el-table-column>
      <el-table-column label="岗位名称" align="center" prop="tstgGwmc" >
        <template slot-scope="scope">
        {{ getgwxxLabel(scope.row.tstgGwmc) }} 
        </template>
      </el-table-column>
      <el-table-column label="岗位等级" align="center" prop="tstgGwdj" />
      <el-table-column label="当前状态" align="center" prop="status" />
      <el-table-column label="添加人" align="center" prop="creator" />
      <el-table-column label="添加时间" align="center" prop="createdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后修改人" align="center" prop="lastmodifier" />
      <el-table-column label="最后修改时间" align="center" prop="lastmodifieddate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastmodifieddate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['admin:tstg:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['admin:tstg:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改调社调岗对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生姓名" prop="tstgXsxm">
          <el-input v-model="form.tstgXsxm" placeholder="请输入学生姓名" />
        </el-form-item>
        <el-form-item label="学生校号" prop="tstgXsxh">
          <el-input v-model="form.tstgXsxh" placeholder="请输入学生校号" @change="getUser()"/>
        </el-form-item>
        <el-form-item label="所属学院" prop="tstgSsxy">
          <el-select v-model="form.tstgSsxy" placeholder="请选择所属学院">
            <el-option
              v-for="dict in deptList"
              :key="dict.deptId"
              :label="dict.deptName"
              :value="dict.deptId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所学专业" prop="tstgSxzy">
          <el-input v-model="form.tstgSxzy" placeholder="请输入所学专业" />
        </el-form-item>
        <el-form-item label="学社类别" prop="tstgXslb">
          <el-select v-model="form.tstgXslb" placeholder="请选择学社类别">
            <el-option
              v-for="dict in xslbList"
              :key="dict.id"
              :label="dict.xsglXslb"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学社名称" prop="tstgXsmc">
          <el-select v-model="form.tstgXsmc" placeholder="请选择学社" @change="getLbGw" >
            <el-option
              v-for="dict in xsxxList"
              :key="dict.id"
              :label="dict.xsxxXsmc"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="岗位名称" prop="tstgGwmc">
          <el-select v-model="form.tstgGwmc" placeholder="请选择岗位" @change="getDj">
            <el-option
              v-for="dict in listGwxx"
              :key="dict.id"
              :label="dict.gwxxGwmc"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="岗位等级" prop="tstgGwdj">
          <el-select v-model="form.tstgGwdj" placeholder="请选择岗位等级">
            <el-option
              v-for="dict in dict.type.xveshe_grade"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="添加人" prop="creator">
          <el-input v-model="form.creator" placeholder="请输入添加人" />
        </el-form-item>
        <el-form-item label="添加时间" prop="createdate">
          <el-date-picker clearable
            v-model="form.createdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择添加时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最后修改人" prop="lastmodifier">
          <el-input v-model="form.lastmodifier" placeholder="请输入最后修改人" />
        </el-form-item>
        <el-form-item label="最后修改时间" prop="lastmodifieddate">
          <el-date-picker clearable
            v-model="form.lastmodifieddate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最后修改时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTstg, getTstg, delTstg, addTstg, updateTstg } from "@/api/admin/tstg";
import {getUser} from "@/api/system/user";
import { allocatedUserList } from "@/api/system/role";
import {listDeptById} from "@/api/system/dept";
import {listXslb}from "@/api/admin/xslb"
import {listXsxx,getXsxx}from "@/api/admin/xsxx"
import {listGwxx,getGwxx}from "@/api/admin/gwxx"

export default {
  name: "Tstg",
  dicts: ['xveshe_enterprise', 'xveshe_specialty', 'xveshe_grade', 'xveshe_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      stList:[],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 调社调岗表格数据
      tstgList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //学院信息
      deptList:[],
      //学社类别
      xslbList:[],
      //岗位信息
      listGwxx:[],
      //学社信息
      xsxxList:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tstgXsxm: null,
        tstgXsxh: null,
        tstgSsxy: null,
        tstgSxzy: null,
        tstgXslb: null,
        tstgXsmc: null,
        tstgGwmc: null,
        tstgGwdj: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getSt();
    this.getDept(201);
    this.getlistGwxx();
    this.getXsxxList();
    this.getxslb();
  },
  methods: {
    /** 查询调社调岗列表 */
    getList() {
      this.loading = true;
      listTstg(this.queryParams).then(response => {
        this.tstgList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },//获取岗位等级
    getDj(){
      getGwxx(this.form.tstgGwmc).then(response => {
        this.form.tstgGwdj = response.data.gwxxGwdj;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tstgXsxm: null,
        tstgXsxh: null,
        tstgSsxy: null,
        tstgSxzy: null,
        tstgXslb: null,
        tstgXsmc: null,
        tstgGwmc: null,
        tstgGwdj: null,
        status: null,
        creator: null,
        createdate: null,
        lastmodifier: null,
        lastmodifieddate: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加调社调岗";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTstg(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改调社调岗";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTstg(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTstg(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除调社调岗编号为"' + ids + '"的数据项？').then(function() {
        return delTstg(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('admin/tstg/export', {
        ...this.queryParams
      }, `tstg_${new Date().getTime()}.xlsx`)
    },
    /** 查询学社类别 */
    getxslb() {
      this.loading = true;
      listXslb(this.queryParams).then(response => {
        this.xslbList = response.rows;
      });
    },
    //获取学社信息
    getXsxxList(){
      listXsxx().then(response => {
        this.xsxxList = response.rows;
      });
    },
    //获取学社类别和下属岗位列表
    getLbGw(){
      getXsxx(this.form.tstgXsmc).then(response=>{
        this.form.tstgXslb=response.data.xsxxXslb
      })
      const queryParams={
        gwxxDyxs: this.form.tstgXsmc
      }
      listGwxx(queryParams).then(response => {
        this.form.tstgGwmc=null;
        this.listGwxx = response.rows;
      })
    },
    //获取岗位信息
    getlistGwxx(){
      listGwxx().then(response => {
        this.listGwxx = response.rows;
      });
    }, 
     //获取学生列表
     getSt(){
      const queryParams= {
        roleId: 102
      }
      allocatedUserList(queryParams).then(res => {
        this.stList = res.rows;
      });
    },//查询部门/学院信息
    getDept(id){
      listDeptById(id).then(response => {
        this.deptList = response.data;
        this.loading = false;
      });
    },
    getdeptLabel(id) {  
      const map = this.deptList.reduce((acc, item) => {  
        acc[item.deptId] = item.deptName;
        return acc;  
      }, {});  
      return map[id] || 'Unknown Label';  
    }  ,
    //获取用户信息
    getUser(){
      const userid=this.getUserid(this.form.tstgXsxh)
      getUser(userid).then(response => {
        this.form.tstgSxzy=response.data.dept.deptName
        this.form.tstgSsxy=response.data.dept.parentId
         this.form.tstgXsxm=response.data.nickName
      })
    }, 
    getUserid(userName) {  
      // 创建一个映射函数，该函数将ID映射到标签  
      const map = this.stList.reduce((acc, item) => {  
        acc[item.userName] = item.userId;
        return acc;  
      }, {});  
      // 返回一个函数，该函数接受一个ID并返回相应的标签  
      return map[userName] || 'Unknown Label';  
    }  ,
    getLabel(id) {  
      // 创建一个映射函数，该函数将ID映射到标签  
      const map = this.xslbList.reduce((acc, item) => {  
        acc[item.id] = item.xsglXslb;  
        return acc;  
      }, {});  
      // 返回一个函数，该函数接受一个ID并返回相应的标签  
      return map[id] || 'Unknown Label';  
    }  ,
    getgwxxLabel(id) {  
      // 创建一个映射函数，该函数将ID映射到标签  
      const map = this.listGwxx.reduce((acc, item) => {  
        acc[item.id] = item.gwxxGwmc;
        return acc;  
      }, {});  
      // 返回一个函数，该函数接受一个ID并返回相应的标签  
      return map[id] || 'Unknown Label';  
    }   ,
    getxsxxLabel(id) {  
      // 创建一个映射函数，该函数将ID映射到标签  
      const map = this.xsxxList.reduce((acc, item) => {  
        acc[item.id] = item.xsxxXsmc;
        return acc;  
      }, {});  
      // 返回一个函数，该函数接受一个ID并返回相应的标签  
      return map[id] || 'Unknown Label';  
    }  
  }
};
</script>
